package logic

import (
	"context"
	"goods_srv/goods"

	"api/internal/svc"
	"api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type CreateGoodsLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewCreateGoodsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreateGoodsLogic {
	return &CreateGoodsLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *CreateGoodsLogic) CreateGoods(req *types.CreateGoodsRequest) (resp *types.RegMsgResponse, err error) {
	// todo: 新增商品

	//新增商品
	createGoods, err := l.svcCtx.GoodsSrv.CreateGoods(l.ctx, &goods.CreateGoodsRequest{
		GoodsTitle:  req.GoodsTitle,
		GoodsPrice:  req.GoodsPrice,
		GoodsStock:  req.GoodsStock,
		GoodsStatus: int64(req.GoodsStatus),
		GoodsAvatar: req.GoodsAvatar,
	})
	if err != nil {
		return &types.RegMsgResponse{
			Cre:  -1,
			Msg:  "服务器错误",
			Data: nil,
		}, err
	}

	return &types.RegMsgResponse{
		Cre: 0,
		Msg: "新增商品成功",
		Data: map[string]interface{}{
			"goodsId": createGoods.GoodsId,
		},
	}, err
}
